﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using Cn.Cellula.Biz;
using System.Reflection;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product {
    public partial class List : PageSupport,ICellulaBiz {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadProducts();
            }
        }
        private void LoadProducts() {
            String hql = " from VProductEditListEO eo where eo.OwnerKey='"+this.OwnerKey+"' ";
            if (!String.IsNullOrEmpty(this.ddl_Category.SelectedValue)) {
                if (this.ddl_Category.SelectedValue == TxtDef.Category.DDLValueEmpty) {
                    hql += " and (eo.CategoryName is null or eo.CategoryName ='') ";
                } else {
                    ICategoryBiz cbiz = BizFactory.Get<ICategoryBiz>();
                    IList<String> ckeys = cbiz.LoadDescendantsKeys(null, CodeTypeDef.Category.Product, this.ddl_Category.SelectedValue);
                    if (ckeys != null && ckeys.Count > 0) {
                        hql += " and (eo.CategoryId = '" + this.ddl_Category.SelectedValue + "' ";
                        for (int i = 0; i < ckeys.Count; i++) {
                            String k = ckeys[i];
                            hql += " or eo.CategoryId='" + k + "' ";
                        }
                        hql += " ) ";
                    } else {
                        hql += " and eo.CategoryId = '" + this.ddl_Category.SelectedValue + "' ";
                    }
                }
            }
            if (!String.IsNullOrEmpty(this.ddl_Del.SelectedValue)) {
                hql += " and eo.FlgDel =" + this.ddl_Del.SelectedValue + " ";
            }
            if (!String.IsNullOrEmpty(this.ddl_ListStatus.SelectedValue)) {
                hql += " and eo.ListStatus =" + this.ddl_ListStatus.SelectedValue + " ";
            }
            if (!String.IsNullOrEmpty(this.txt_ProductName.Text)) {
                hql += " and eo.ProductName like '%" + this.txt_ProductName.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.txt_StartDate.Text)) {
                hql += " and eo.LastDate >= '" + this.txt_StartDate.Text + "' ";
            }
            if (!String.IsNullOrEmpty(this.txt_EndDate.Text)) {
                hql += " and eo.LastDate <= '" + this.txt_EndDate.Text + "' ";
            }
            if (!String.IsNullOrEmpty(this.txt_Model.Text)) {
                hql += " and eo.ProductModel like '%" + this.txt_Model.Text + "%' ";
            }
            if (this.ddl_HasPic.SelectedValue == "0") {
                hql += " and eo.PicCount<1 ";
            } else if (this.ddl_HasPic.SelectedValue == "1") {
                hql += " and eo.PicCount>0 ";
            }
            if (this.ddl_Marketplace.SelectedValue == "0") {
                hql += " and eo.MarketCount<1 ";
            } else if(this.ddl_Marketplace.SelectedValue=="1") {
                hql += " and eo.MarketCount>0 ";
            }
            hql += " order by eo.LastDate desc ";
            IList<VProductEditListEO> list = biz.Find<VProductEditListEO>(hql);
            this.gvProduct.DataSource = list;
            this.gvProduct.DataBind();
        }

        private void InitForm() {
            this.lab_CategoryData.Text = "<script type=\"text/javascript\">var CategoryData = " + BizFactory.Get<ICategoryBiz>().LoadCategory2Json(this.OwnerKey, CodeTypeDef.Category.Market, null) + ";</script>";

            ICategoryBiz cbiz = BizFactory.Get<ICategoryBiz>();

            this.ddl_Marketplace.DataTextField = "Text";
            this.ddl_Marketplace.DataValueField = "Value";
            this.ddl_Marketplace.Items.Insert(0, new ListItem("无卖场", "0"));
            this.ddl_Marketplace.Items.Insert(0, new ListItem("有卖场", "1"));
            this.ddl_Marketplace.Items.Insert(0, new ListItem(TxtDef.DDLTextAll, ""));


            this.ddl_HasPic.DataTextField = "Text";
            this.ddl_HasPic.DataValueField = "Value";
            this.ddl_HasPic.Items.Insert(0, new ListItem("无图片", "0"));
            this.ddl_HasPic.Items.Insert(0, new ListItem("有图片", "1"));
            this.ddl_HasPic.Items.Insert(0, new ListItem(TxtDef.DDLTextAll, ""));

            this.ddl_Category.DataTextField = "Text";
            this.ddl_Category.DataValueField = "Value";
            this.ddl_Category.DataSource = cbiz.LoadCatalog2ListItem(this.OwnerKey, CodeTypeDef.Category.Product, "..", null, true);
            this.ddl_Category.DataBind();
            this.ddl_Category.Items.Insert(0, new ListItem(TxtDef.Category.DDLTextEmpty, "0"));
            this.ddl_Category.Items.Insert(0, new ListItem(TxtDef.DDLTextAll, ""));

            this.txt_EndDate.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            this.txt_StartDate.Text = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            LoadProducts();
        }

        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("AddCaSelect.aspx");
        }

        protected void btn_EOUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvProduct, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                Response.Redirect("Edit.aspx?id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_EODel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvProduct, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.DeleteProduct(keys);
                BizFactory.Get<ILogBiz>().LogDelete("产品", keys.Count, this.Actor, Request);
                this.LoadProducts();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }

        protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvProduct.PageIndex = e.NewPageIndex;
            LoadProducts();
        }

        #region ICellulaBiz 成员

        public string ModelName {
            get { return CodeTypeDef.Module.Product; }
        }

        #endregion

        protected void btn_List_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvProduct, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.SetList(keys);
                BizFactory.Get<ILogBiz>().Info("设置产品上架", this.Actor, Request);
                this.LoadProducts();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }

        protected void btn_Delist_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvProduct, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.SetDelist(keys);
                BizFactory.Get<ILogBiz>().Info("设置产品下架", this.Actor, Request);
                this.LoadProducts();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
    }
}
